package com.longsec.controller;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.longsec.qq.ILoginService;
import com.longsec.qq.vo.QrInfoVo;
import com.longsec.qq.vo.VerifyVo;
import com.longsec.qrcode.QRCodeUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("qq")
public class QQController {
	
	private static final String SERSSION_ID_LOGIN_QQ = "SERSSION_ID_LOGIN_QQ";
	private static final String SERSSION_ID_CACHED_QQ = "SERSSION_ID_CACHED_QQ";
	
	@Autowired//QQ登入服务
	private ILoginService loginService;
	
	@Autowired//会话
	private HttpSession session;
	
	/**
	 * 首次登入
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("")
	public void loginQr(HttpServletResponse response) throws Exception{
		QrInfoVo loginQr = getCached();
		if (loginQr == null) {
			loginQr = loginService.loginQr();			
		}
		responseQr(response, loginQr);
	}
	
	/**
	 * 刷新二维码
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("updateqr")
	public void updateQr(HttpServletResponse response) throws Exception{
		Object info = session.getAttribute(SERSSION_ID_LOGIN_QQ);
		if (info == null) {
			log.info("未找到会话信息-重新获取二维码");
			loginQr(response);
		}else{
			log.info("找到会话信息-刷新二维码");
			QrInfoVo loginQr = (QrInfoVo)info;
			QrInfoVo updateQr = getCached();
			if (updateQr == null) {
				updateQr = loginService.updateQr(loginQr.getCookies());
			}
			responseQr(response, updateQr);
		}
	}
	
	/**
	 * 刷新二维码
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("updateMyqr")
	public void updateMyQr(HttpServletResponse response) throws Exception{
		Object info = session.getAttribute(SERSSION_ID_LOGIN_QQ);
		if (info == null) {
			log.info("未找到会话信息-重新获取二维码");
			loginQr(response);
		}else{
			log.info("找到会话信息-刷新二维码");
			QrInfoVo loginQr = (QrInfoVo)info;
			QrInfoVo updateQr = getCached();
			if (updateQr == null) {
				updateQr = loginService.updateQr(loginQr.getCookies());
			}
			responseQr(response, updateQr);
		}
	}

	@RequestMapping("verifyqr")
	public Object verifyQr(){
		Object info = session.getAttribute(SERSSION_ID_LOGIN_QQ);
		if (info == null) {
			return "";
		}
		QrInfoVo loginQr = (QrInfoVo)info;
		VerifyVo verify = loginService.verify(loginQr.getPtLoginSig(), loginQr.getPtqrtoken(), loginQr.getCookies());
		return verify;
	}
	
	/**
	 * 响应二维码
	 * @param response
	 * @param qrInfoVo
	 * @throws Exception 
	 */
	private void responseQr(HttpServletResponse response,QrInfoVo qrInfoVo) throws Exception{
		//--- Begin 自定义美化二维码 ---
		String content = QRCodeUtil.decode(qrInfoVo.getImg());
		String path = ResourceUtils.getFile("classpath:1.jpeg").getPath();
		byte[] imgByte = QRCodeUtil.encodeBytes(content, path, true);
		qrInfoVo.setImg(imgByte);
		//--- End 自定义美化二维码 ---
		
		session.setAttribute(SERSSION_ID_LOGIN_QQ, qrInfoVo);
		OutputStream out = response.getOutputStream();
		response.setContentType("image/jpeg");
        out.write(qrInfoVo.getImg());
        out.flush();
        out.close();
	}
	
	private QrInfoVo getCached(){
		Object reqTime = session.getAttribute(SERSSION_ID_CACHED_QQ);
		if (reqTime != null) {
			if ((System.currentTimeMillis() - (Long)reqTime) < (5*1000)) {
				return (QrInfoVo)session.getAttribute(SERSSION_ID_LOGIN_QQ);
			}
		}
		session.setAttribute(SERSSION_ID_CACHED_QQ, System.currentTimeMillis());
		return null;
	}
	
	
	
}
